package com.example.cabinetcontrol.data;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.example.cabinetcontrol.port.SerialPortService;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2017/11/11 0011.
 */

public class MyData {

    Context context;

    public MyData(Context context){
        this.context = context ;
    }

    public long getDate(){
        Date curDate =  new Date(System.currentTimeMillis());
        Log.d("Context","时间："+curDate.getTime()) ;
        return curDate.getTime();
    }


    //如果service未运行则开启
    public void OpenService(){
        if (isServiceRunning(context, "com.example.administrator.whitehorsecabinet.socket.SocketSerialPortService"))
        {
        }
        else {
            Log.d("Context","服务运行关闭中");
            Intent intent = new Intent(context, SerialPortService.class);
            context.startService(intent);
        }
    }
    //检测service是否在运行
    public static boolean isServiceRunning(Context context, String ServiceName) {
        if (("").equals(ServiceName) || ServiceName == null)
            return false;
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(30);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().toString()
                    .equals(ServiceName)) {
                return true;
            }
        }
        return false;
    }

    //间距多少时间内点击无效
    private static long lastClickTime;
    public synchronized static boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


}
